<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="店铺名称">
                <a-input allow-clear placeholder="请输入店铺名称" v-model="queryParam.store_name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="用户昵称">
                <a-input allow-clear placeholder="请输入用户昵称" v-model="queryParam.nickname"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号码">
                <div class="select">
                  <a-input placeholder="请输入手机号码" v-model="queryParam.mobile"></a-input>
                  <a-button type="primary" @click="getList">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未开启 </a-radio-button>
          <a-radio-button value="1"> 已开启 </a-radio-button>
        </a-radio-group>
        <a-button type="primary" @click="toSetting(-1)">默认设置</a-button>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.logo" alt="" />
            </div>
            <div class="info">
              <span>{{ record.store_name }}</span>
              <!-- <span>{{ record.tel }}</span> -->
            </div>
          </div>
        </div>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope == 1 ? 'open' : 'close'"></span>
            <span>{{ scope == 1 ? '已启用' : '未开启' }}</span>
          </div>
        </div>
        <div slot="action" slot-scope="scope">
          <a-button type="link" size="small" @click="toSetting(scope)">设置</a-button>
          <a-divider type="vertical" style="margin: 0 1px" />
          <a-dropdown>
            <a-button type="link" size="small"> 更多 <a-icon type="down" /></a-button>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确认关闭吗？" :disabled="scope.store_share_switch == 0" @confirm="changeStatus(scope, '0')">
                  <a-button type="link" :disabled="scope.store_share_switch == 0">关闭</a-button>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确认开启吗？" :disabled="scope.store_share_switch == 1" @confirm="changeStatus(scope, '1')">
                  <a-button type="link" :disabled="scope.store_share_switch == 1">开启</a-button>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
        <span slot="commission" slot-scope="scope">
          {{ scope.store_share_value }}% <span v-if="scope.use_default" style="color:#65c817"><br>系统默认值</span>
        </span>
        <span slot="type" slot-scope="text">
          {{ text == 0 ? '个人' : text == 1 ? '企业' : '个体工商户' }}
        </span>
      </a-table>
    </a-card>
    <!-- 设置弹窗 -->
    <a-modal
      title="店铺分销设置"
      width="800px"
      v-model="showSetting"
      @ok="editSetting"
      :confirmLoading="btnLoading"
      :destroyOnClose="true"
    >
      <a-form-model
        ref="settingForm"
        :model="settingForm"
        :rules="settingRules"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-form-model-item label="是否开启" prop="store_share_switch">
          <a-radio-group v-model="settingForm.store_share_switch">
            <a-radio value="1"> 开启 </a-radio>
            <a-radio value="0"> 关闭 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="一级佣金" prop="store_share_value">
          <a-input
            addon-after="%"
            v-model="settingForm.store_share_value"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { shopDistributionList, editShopSetting } from '@/api/saas/alliance'
import tabList from '@/views/saas/components/tabList.vue'
import { cyyUploadImg } from '@/components/CyyUi'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '店铺信息',
    scopedSlots: {
      customRender: 'name',
    },
    width: 250,
  },
  {
    title: '管理员',
    dataIndex: 'nickname',
    width:150,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '一级佣金',
    scopedSlots: {
      customRender: 'commission',
    },
    sorter: (a, b) => a.store_share_value - b.store_share_value,
  },
  {
    title: '累计共享收益',
    dataIndex: 'income',
    sorter: (a, b) => a.income - b.income,
  },
  {
    title: '累计支出佣金',
    dataIndex: 'expenditure',
    sorter: (a, b) => a.expenditure - b.expenditure,
  },
  {
    title: '状态',
    dataIndex: 'store_share_switch',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 160,
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  components: {
    tabList,
    cyyUploadImg,
  },
  data() {
    return {
      // 搜索参数
      queryParam: {
        page: 1,
        mobile: '',
        store_name: '',
        nickname: '',
        status: '-1',
      },
      columns,
      list: [],
      // 是否显示设置分销弹窗
      showSetting: false,
      // 新增 编辑商户的表单
      settingForm: {
        store_id: '',
        store_share_switch: '',
        store_share_type: '0',
        store_share_value: '',
      },
      settingRules: {
        store_share_switch: [{ required: true, message: '请选择是否开启', trigger: 'change' }],
        store_share_value: [{ required: true, message: '请输入一级佣金', trigger: 'blur' }],
      },
      btnLoading: false,
      defaultSet: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await shopDistributionList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.data
        this.totalCount = res.data.totalCount
        this.defaultSet = res.data.defaultSet
      }
      hide()
    },
    // 点击创建商户
    editSetting() {
      this.$refs['settingForm'].validate(async (valid) => {
        if (valid) {
          this.btnLoading = true
          const res = await editShopSetting(this.settingForm)
          this.btnLoading = false
          if (res.code == 0) {
            this.showSetting = false
            this.getList()
          }
        } else {
          return false
        }
      })
    },
    toSetting(scope) {
      if(scope === -1){
        this.settingForm.store_id = -1
        this.settingForm.store_share_switch = this.defaultSet.store_share_switch ? this.defaultSet.store_share_switch : null
        this.settingForm.store_share_type = '0'
        this.settingForm.store_share_value = this.defaultSet.store_share_value ? this.defaultSet.store_share_value : null
        this.showSetting = true
        return;
      }
      this.settingForm.store_id = scope.id
      this.settingForm.store_share_switch = scope.store_share_switch
      this.settingForm.store_share_type = '0'
      this.settingForm.store_share_value = scope.store_share_value
      this.showSetting = true
    },
    async changeStatus(scope, type) {
      const res = await editShopSetting({ store_id: scope.id, store_share_switch: type })
      if (res.code == 0) {
        this.$message.success('修改成功')
        this.getList()
      }
    },
  },
}
</script>
<style lang="less" scoped>
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.pic {
  width: 48px;
  min-width: 48px;
  height: 48px;
  border-radius: 4px;
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #ccc;
  }
  .open {
    background-color: #65c817;
  }
}
</style>
